﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO.Compression;

namespace PcapDemo
{
    public class MessageAnalyse
    {
        private byte[] _originalBytes;
        private byte[] _headerBytes;
        private byte[] _contextBytes;

        private string _header = "";
        private string _context = "";

        public MessageAnalyse(byte[] bytes)
        {
            _originalBytes = bytes;
            if (_originalBytes == null)
            {
                return;
            }
            int j = 0;
            for (int i = 0; i < _originalBytes.Length - 3; i++)
            {
                if (_originalBytes[i] == '\r' && _originalBytes[i + 1] == '\n'
                    && _originalBytes[i + 2] == '\r' && _originalBytes[i + 3] == '\n')
                {
                    j = i + 3;
                }
            }
            _headerBytes = _originalBytes.Take(j).ToArray();
            _header = Encoding.Default.GetString(_headerBytes);
            _contextBytes = _originalBytes.Skip(j + 1).ToArray();
        }

        public string GetHeader()
        {
            return _header;
        }

        public string GetContext()
        {
            var encodeType = "";
            var lines = _header.Split(new[] { "\r\n" }, StringSplitOptions.None);
            foreach (var p in lines)
            {
                if (p.Split(':')[0] == "Content-Encoding"
                    || p.Split(':')[0] == "Content-Type")
                {
                    encodeType = encodeType == "" ? p.Split(':')[1] : (encodeType + "," + p.Split(':')[1]);
                }
            }
            encodeType = encodeType.ToLower();
            //先解压
            if (encodeType.Contains("gzip"))
            {
                _contextBytes = GzipHelper.Decompress(_contextBytes);
            }
            //分析文件类型
            if (encodeType.Contains("image"))
            {
                var fileType = Regex.Match(encodeType, "(?<=image/)[a-zA-Z]*").Value.ToLower();
                Random r = new Random();
                switch (fileType)
                {
                    case "jpeg":
                        fileType = "jpg";
                        break;
                    case "png":
                        fileType = "png";
                        break;
                    case "tiff":
                        fileType = "tif";
                        break;
                    case "gif":
                        fileType = "gif";
                        break;
                    case "x-icon":
                        fileType = "x-icon";
                        break;
                    default:
                        return _context = Encoding.Default.GetString(_contextBytes);
                }
                string path = Convert.ToString(r.Next()) + "." + fileType;
                File.WriteAllBytes(path, _contextBytes);
                return path;
            }
            return _context = Correction(Encoding.Default.GetString(_contextBytes));
        }

        public string GetMsg()
        {
            return GetHeader() + "\r\n" + GetContext();
        }

        public string Correction(string context)
        {
            var charset = Regex.Match(context.ToLower(), "<meta[^>]*charset[^>=]*=[^>\"]*\"[^\"]*\"").Value;
            charset = charset.Trim();
            if (charset.Contains("utf-8"))
            {
                _context = Encoding.UTF8.GetString(_contextBytes);
                return _context;
            }
            return context;
        }
    }
}
